/**
 * 
 */
package com.mindtree.vi.presentation.servlet;

import java.util.ArrayList;
import java.util.List;

import com.mindtree.vi.business.questionsubmission.QuestionManager;
import com.mindtree.vi.dbaccess.datatypes.problem.DifficultyLevel;
import com.mindtree.vi.dbaccess.datatypes.problem.Problem;
import com.mindtree.vi.dbaccess.datatypes.problem.ProblemDesc;
import com.mindtree.vi.dbaccess.datatypes.problem.ProblemType;
import com.mindtree.vi.presentation.annotation.ReferencedBy;
import com.mindtree.vi.xml.XmlProblem;
import com.mindtree.vi.xml.XmlProblemDesc;
import com.mindtree.vi.xml.XmlProblemRequest;
import com.mindtree.vi.xml.XmlProblemResponse;

/**
 * @author venkatesh
 * 
 */
public class SubmitCodeServlet extends
		BaseHttpServlet<XmlProblemRequest, XmlProblemResponse> {

	private static final long serialVersionUID = -8977153384561209217L;

	@ReferencedBy("questionManager")
	private QuestionManager questionManager;

	@Override
	public void process() {

		XmlProblem xmlProblem = getRequest().getProblem();

		Problem problem = new Problem();
		ProblemDesc problemDesc = new ProblemDesc();
		XmlProblemDesc xmlProblemDesc  = null;
		if(xmlProblem!=null){
			problem.setDifficultyLevel(DifficultyLevel.valueOf(xmlProblem
					.getDifficultyLevel()));
			xmlProblemDesc = xmlProblem.getProblemDescription();
			problem
					.setProblemType(ProblemType
							.valueOf(xmlProblem.getProblemType()));
		}
			
		if(xmlProblemDesc!=null){
			problemDesc.setClassName(xmlProblemDesc
					.getClassName());
			problemDesc.setMethodArguments(xmlProblemDesc
					.getParameters());
			problemDesc.setMethodName(xmlProblemDesc
					.getMethodName());
			problemDesc.setMethodReturnType(xmlProblemDesc
					.getReturnType());
			problemDesc.setProblemText(xmlProblemDesc
					.getProblemText());
		}
		
		problem.setProblemDescription(problemDesc);
		
		problem = questionManager.submitQuestion(problem);
		XmlProblemResponse response = new XmlProblemResponse();
		response.setMessage("SUCCESSFully submitted!");
		xmlProblem = new XmlProblem();
		xmlProblem.setProblemId(problem.getProblemId());
		List<XmlProblem> problemList = new ArrayList<XmlProblem>();
		problemList.add(xmlProblem);
		response.setProblems(problemList);
		setResponse(response);

	}

}